﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YXShop.IDAL.SystemInfo;

namespace YXShop.SQLServerDAL.SystemInfo
{
    /// <summary>
    /// 数据访问类MouldType。
    /// </summary>
    public class MouldType : IMouldType
    {
        public MouldType()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from yxs_MouldType");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return ChangeHope.DataBase.SQLServerHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.SystemInfo.MouldType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_MouldType(");
            strSql.Append("Name,Description,AddTime,isstoremould)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Description,@AddTime,@isstoremould)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.VarChar,5000),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                                        new SqlParameter("@isstoremould",SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.IsStoreMould;

            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(YXShop.Model.SystemInfo.MouldType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_MouldType set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Description=@Description,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("isstoremould=@isstoremould");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.VarChar,5000),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                    new SqlParameter("@isstoremould",SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.AddTime;
            parameters[4].Value = model.IsStoreMould;

            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_MouldType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.SystemInfo.MouldType GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,Description,AddTime,isstoremould from yxs_MouldType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            YXShop.Model.SystemInfo.MouldType model = new YXShop.Model.SystemInfo.MouldType();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isstoremould"].ToString() != "")
                {
                    model.IsStoreMould = int.Parse(ds.Tables[0].Rows[0]["isstoremould"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList()
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_MouldType [where] 1=1 [order by] id desc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        
        #endregion  成员方法
    }
}

